package my.http;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.util.concurrent.ConcurrentHashMap;

import my.LogUtil;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MyParams {

	public final static String PARAMS_LOG ="params_log";

	private final ConcurrentHashMap<String, String> urlParams = new ConcurrentHashMap<String, String>();
	private final ConcurrentHashMap<String, File> paramsFiles = new ConcurrentHashMap<String, File>();
	private final ConcurrentHashMap<String, Object> paramsObject= new ConcurrentHashMap<String, Object>();
	private  String submitJson = "";

	public ConcurrentHashMap<String, String> getUrlParams() {
		return urlParams;
	}

	public void putJson(String json) {
		submitJson = json;
	}


	public void put(String key, String value) {
		if (key != null && value != null) {
			urlParams.put(key, value);
		}
	}
	public void put(String key, Object value) {
		if (key != null && value != null) {
			urlParams.put(key, String.valueOf(value));
		}
	}

	public void add(String key, String value) {
		if (key != null && value != null) {
			urlParams.put(key,value);
		}
	}
	public void add(String key, Object value) {
		if (key != null && value != null) {
			urlParams.put(key, String.valueOf(value));
		}
	}

	public void put(String key, File file) {
		if (key != null && file != null) {
			paramsFiles.put(key,file);
		}
	}

	public void putObj(String key, Object obj) {
		if (key != null && obj != null) {
			paramsObject.put(key,obj);
		}
	}



	public HttpUrl.Builder getHttpUrlBody(String url){
		HttpUrl.Builder builder = HttpUrl.parse(url).newBuilder();
		StringBuilder stringBuilder = new StringBuilder();
		for(String key:urlParams.keySet()){
			builder.addQueryParameter(key,urlParams.get(key));
			stringBuilder.append(key).append("=").append(urlParams.get(key)).append("&");
		}
		LogUtil.i(MyHttpUtils.tag,stringBuilder.toString());
		return builder;
	}

	public String getRequestString(){
		StringBuilder stringBuilder = new StringBuilder();
		for(String key:urlParams.keySet()){
			stringBuilder.append(key).append("=").append(urlParams.get(key)).append("&");
		}
		LogUtil.i(MyHttpUtils.tag,stringBuilder.toString());
		String url = stringBuilder.toString();
		if(url.endsWith("&")){
			url = url.substring(0,url.length()-1);
		}
		return url;
	}

	public RequestBody getRequestBody(){
		FormBody.Builder builder  = new FormBody.Builder();
		StringBuilder stringBuilder = new StringBuilder();
		for(String key:urlParams.keySet()){
			builder.add(key,urlParams.get(key));
			stringBuilder.append(key).append("=").append(urlParams.get(key)).append("&");
		}
		LogUtil.i(MyHttpUtils.tag,stringBuilder.toString());
		return builder.build();
	}

	public RequestBody getJson(){
		RequestBody requestBody  = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),submitJson);
		LogUtil.i(MyHttpUtils.tag,submitJson);
		return requestBody;
	}

	public RequestBody getRequestBodyJson(){
		JSONObject json = new JSONObject();

//		StringBuilder stringBuilder = new StringBuilder();
		for(String key:urlParams.keySet()){
			json.put(key,urlParams.get(key));
//			stringBuilder.append(key).append("=").append(urlParams.get(key)).append("&");
		}

		for(String key:paramsObject.keySet()){
			json.put(key,paramsObject.get(key));
		}

		RequestBody requestBody  = RequestBody.create(json.toJSONString(),MediaType.parse("application/json;charset=utf-8"));
		LogUtil.i(MyHttpUtils.tag,json.toJSONString());
		return requestBody;
	}

	public RequestBody getRequestBodyWithFile(){
		MultipartBody.Builder builder =  new MultipartBody.Builder();
		StringBuilder stringBuilder = new StringBuilder();
		for(String key:urlParams.keySet()){
			builder.addFormDataPart(key,urlParams.get(key));
			stringBuilder.append(key).append("=").append(urlParams.get(key)).append("&");
		}

		String TYPE = "application/octet-stream";
		if(paramsFiles != null && paramsFiles.size() > 0){
			builder .setType(MultipartBody.FORM);
			for(String key:paramsFiles.keySet()){
				File file = paramsFiles.get(key);
				RequestBody fileBody = RequestBody.create(file,MediaType.parse(TYPE));
//				RequestBody fileBody =  RequestBody.create(file,MediaType.parse("multipart/form-data"));
				builder.addFormDataPart(key,file.getName(),fileBody);
				stringBuilder.append(key).append("=").append(file.getName()).append("&");
			}
		}
		LogUtil.i(MyHttpUtils.tag,stringBuilder.toString());
		return builder.build();
	}

	public String getListKeyValueList(){
		StringBuilder builder = new StringBuilder();
		for(String key:urlParams.keySet()){
//			builder.add(key,urlParams.get(key));
			builder.append(key).append("=").append(urlParams.get(key)).append("&");
		}
		if(builder.length() > 0)builder.deleteCharAt(builder.length()-1);
		return builder.toString();
	}
}
